{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<a href=\"https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv\" target=\"_blank\"><img align=\"left\" src=\"data/cover.jpg\" style=\"width: 76px; height: 100px; background: white; padding: 1px; border: 1px solid black; margin-right:10px;\"></a>\n",
    "*This notebook contains an excerpt from the book [Machine Learning for OpenCV](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv) by Michael Beyeler.\n",
    "The code is released under the [MIT license](https://opensource.org/licenses/MIT),\n",
    "and is available on [GitHub](https://github.com/mbeyeler/opencv-machine-learning).*\n",
    "\n",
    "*Note that this excerpt contains only the raw code - the book is rich with additional explanations and illustrations.\n",
    "If you find this content useful, please consider supporting the work by\n",
    "[buying the book](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv)!*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Classifying Iris Species Using Logistic Regression](03.05-Classifying-Iris-Species-Using-Logistic-Regression.ipynb) | [Contents](../README.md) | [Preprocessing Data](04.01-Preprocessing-Data.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Representing Data and Engineering Features\n",
    "\n",
    "In the last chapter, we built our very first supervised learning models and applied them to\n",
    "some classic datasets, such as the **Iris** and the **Boston** datasets. However, in the real world,\n",
    "data rarely comes in a neat `<n_samples x n_features>` **feature matrix** that is part of a\n",
    "pre-packaged database. Instead, it is our own responsibility to find a way to represent the\n",
    "data in a meaningful way. The process of finding the best way to represent our data is\n",
    "known as **feature engineering**, and it is one of the main tasks of data scientists and machine\n",
    "learning practitioners trying to solve real-world problems.\n",
    "\n",
    "I know you would rather jump right to the end and build the deepest neural network\n",
    "mankind has ever seen. But, trust me, this stuff is important! Representing our data in the\n",
    "right way can have a much greater influence on the performance of our supervised model\n",
    "than the exact parameters we choose. And we get to invent our own features, too.\n",
    "\n",
    "In this chapter, we will therefore go over some common feature engineering tasks.\n",
    "Specifically, we want to answer the following questions:\n",
    "- What are some common preprocessing techniques that everyone uses but nobody talks about?\n",
    "- How do we represent categorical variables, such as the names of products, of colors, or of fruits?\n",
    "- How would we even go about representing text?\n",
    "- What is the best way to encode images, and what do SIFT and SURF stand for?\n",
    "\n",
    "## Outline\n",
    "- [Preprocessing Data](04.01-Preprocessing-Data.ipynb)\n",
    "- [Reducing the Dimensionality of the Data](04.02-Reducing-the-Dimensionality-of-the-Data.ipynb)\n",
    "- [Representing Categorical Variables](04.03-Representing-Categorical-Variables.ipynb)\n",
    "- [Representing Text Features](04.04-Represening-Text-Features.ipynb)\n",
    "- [Representing Images](04.05-Representing-Images.ipynb)\n",
    "\n",
    "> The book features a detailed treatment of feature engineering, data preprocessing, and data transformation. Below is a short summary of these topics. For more information, please refer to the book."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Feature engineering\n",
    "\n",
    "Feature engineering comes in two stages:\n",
    "- **Feature selection**: This is the process of identifying important attributes (or\n",
    "  features) in the data. Possible features of an image might be the location of edges,\n",
    "  corners, or ridges. In this chapter, we will look at some of the more advanced\n",
    "  feature descriptors that OpenCV provides, such as the Scale-Invariant Feature\n",
    "  Transform (SIFT) and Speeded Up Robust Features (SURF).\n",
    "- **Feature extraction**: This is the actual process of transforming the raw data into\n",
    "  the desired feature space used to feed a machine learning algorithm as illustrated\n",
    "  in the figure above. An example would be the Harris operator, which allows you\n",
    "  to extract corners (that is, a selected feature) in an image.\n",
    "\n",
    "\n",
    "## Preprocessing data\n",
    "\n",
    "The preprocessing pipeline:\n",
    "\n",
    "- *Data formatting*: The data may not be in a format that is suitable for you to work\n",
    "  with. For example, the data might be provided in a proprietary file format, which\n",
    "  your favorite machine learning algorithm does not understand.\n",
    "- *Data cleaning*: The data may contain invalid or missing entries, which need to be\n",
    "  cleaned up or removed.\n",
    "- *Data sampling*: The data may be far too numerous for your specific purpose,\n",
    "  forcing you to sample the data in a smart way.\n",
    "  \n",
    "\n",
    "## Transforming data\n",
    "\n",
    "Once the data has been preprocessed, you are ready for the actual feature engineering: to transform the\n",
    "preprocessed data to fit your specific machine learning algorithm. This step usually involves one or more of\n",
    "three possible procedures:\n",
    "\n",
    "- *Scaling*: Machine learning algorithms often require the data to be within a\n",
    "  common range, such as to have zero mean and unit variance. Scaling is the\n",
    "  process of bringing all features (which might have different physical units) into a\n",
    "  common range of values.\n",
    "- *Decomposition*: Datasets often have many more features than you could possibly\n",
    "  process. Feature decomposition is the process of compressing data into a smaller\n",
    "  number of highly informative data components.\n",
    "- *Aggregation*: There may be features that can be aggregated into a single feature\n",
    "  that would be more meaningful to the problem you are trying to solve. For\n",
    "  example, a database might contain the date and time each user logged into a webbased\n",
    "  system. Depending on the task, this data might be better represented by\n",
    "  simply counting the number of logins per user. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Classifying Iris Species Using Logistic Regression](03.05-Classifying-Iris-Species-Using-Logistic-Regression.ipynb) | [Contents](../README.md) | [Preprocessing Data](04.01-Preprocessing-Data.ipynb) >"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
